using UnityEngine;
using QFramework;
using UnityEngine.UI;

namespace QFramework.Example
{
    public partial class UGUIInventoryExample : ViewController
    {
        private void Awake()
        {
            //ItemKit.ResLoadHandler = new MyItemKitResLoadHandler();
            ItemKit.SaverHandler = new MyItemKitSaveHandler();
            ItemKit.LoadItemDatabase("ExampleItemDatabase");
            ItemKit.LoadLanguagePackage("ItemEnglishPackage");

            ItemKit.CreateSlotGroup("道具栏")
                .CreateSlot(Items.item_iron_ingots, 3)
                .CreateSlot(Items.item_wooden_sword, 1)
                .CreateSlot(Items.item_iron_sword, 1)
                .CreateSlot(Items.item_emerald_sword, 1)
                .CreateSlotsByCount(4);

            ItemKit.CreateSlotGroup("背包")
                .CreateSlotsByCount(20);

            ItemKit.CreateSlotGroup("武器")
                .CreateSlot(null, 0, out var weaponSlot)
                .Condition(item => item.GetBoolean("IsWeapon"));

            ItemKit.CreateSlotGroup("宝箱1")
                .CreateSlotsByCount(6);
            ItemKit.CreateSlotGroup("宝箱2")
                .CreateSlotsByCount(2)
                .CreateSlot(Items.item_iron_ingots, 2)
                .CreateSlotsByCount(3);

            weaponSlot.OnChanged.Register(() =>
            {
                if (weaponSlot.Item != null)
                {
                    Debug.Log($"切换武器: {weaponSlot.Item.GetName}");
                }
            }).UnRegisterWhenGameObjectDestroyed(gameObject);
        }
        private void Start()
        {
            TreasureBoxExample.Hide();
            ItemKit.Load();

            Add1.onClick.AddListener(() =>
            {
                var result = ItemKit.GetSlotGroupByKey("道具栏").AddItem(Items.item_iron_ingots, 20);
                if (!result.Succeed)
                {
                    if (result.MessageType == SlotGroup.MessageType.Full)
                    {
                        Debug.Log("道具栏满了");
                    }
                }
            });
            Sub1.onClick.AddListener(() =>
            {
                ItemKit.GetSlotGroupByKey("道具栏").SubItem(Items.item_iron_ingots);
            });

            Add2.onClick.AddListener(() =>
            {
                var result = ItemKit.GetSlotGroupByKey("道具栏").AddItem(Items.item_wooden_sword, 1);
                if (!result.Succeed)
                {
                    if (result.MessageType == SlotGroup.MessageType.Full)
                    {
                        Debug.Log("道具栏满了");
                    }
                }
            });
            Sub2.onClick.AddListener(() =>
            {
                ItemKit.GetSlotGroupByKey("道具栏").SubItem(Items.item_wooden_sword);
            });

            Add3.onClick.AddListener(() =>
            {
                var result = ItemKit.GetSlotGroupByKey("道具栏").AddItem(Items.item_iron_sword, 1);
                if (!result.Succeed)
                {
                    if (result.MessageType == SlotGroup.MessageType.Full)
                    {
                        Debug.Log("道具栏满了");
                    }
                }
            });
            Sub3.onClick.AddListener(() =>
            {
                ItemKit.GetSlotGroupByKey("道具栏").SubItem(Items.item_iron_sword);
            });

            Add4.onClick.AddListener(() =>
            {
                var result = ItemKit.GetSlotGroupByKey("道具栏").AddItem(Items.item_emerald_sword, 1);
                if (!result.Succeed)
                {
                    if (result.MessageType == SlotGroup.MessageType.Full)
                    {
                        Debug.Log("道具栏满了");
                    }
                }
            });
            Sub4.onClick.AddListener(() =>
            {
                ItemKit.GetSlotGroupByKey("道具栏").SubItem(Items.item_emerald_sword);
            });

            Add5.onClick.AddListener(() =>
            {
                var result = ItemKit.GetSlotGroupByKey("道具栏").AddItem("item_5", 1);
                if (!result.Succeed)
                {
                    if (result.MessageType == SlotGroup.MessageType.Full)
                    {
                        Debug.Log("道具栏满了");
                    }
                }
            });
            Sub5.onClick.AddListener(() =>
            {
                ItemKit.GetSlotGroupByKey("道具栏").SubItem("item_5");
            });

            Sub5_10.onClick.AddListener(() =>
            {
                if (!ItemKit.GetSlotGroupByKey("道具栏").SubItem("item_5", 10)) Debug.Log("数量不足");
            });

            BtnTreasureBox1.onClick.AddListener(() =>
            {
                TreasureBoxExample.SetActive(!TreasureBoxExample.activeSelf);
                TreasureBoxExample.GetComponent<UISlotGroup>().ReLoadSlotsWithKey("宝箱1");
            });

            BtnTreasureBox2.onClick.AddListener(() =>
            {
                TreasureBoxExample.SetActive(!TreasureBoxExample.activeSelf);
                TreasureBoxExample.GetComponent<UISlotGroup>().ReLoadSlotsWithKey("宝箱2");
            });



            BtnLanguage.onClick.AddListener(() =>
            {
                if (ItemKit.CurrentLanguage == ItemKit.DefaultLanguage)
                {
                    ItemKit.LoadLanguagePackage("ItemEnglishPackage");
                }
                else
                {
                    ItemKit.LoadLanguagePackage(ItemKit.DefaultLanguage);
                }
                UpdateLanguageText();
            });
        }
        private void OnApplicationQuit()
        {
            ItemKit.Save();
        }
        private void UpdateLanguageText()
        {
            if (ItemKit.CurrentLanguage == ItemKit.DefaultLanguage)
            {
                BtnLanguage.GetComponentInChildren<Text>().text = "简中=>EN";
            }
            else
            {
                BtnLanguage.GetComponentInChildren<Text>().text = "EN=>简中";
            }
        }
    }
}
